<?php

require_once 'function.php';


//验证输入
if(!isset($_COOKIE['uid']) || !isset($_COOKIE['key']) ){
	print_json(array('error'=>1 , 'msg'=>'Param error'));
	die();
}
$uid = $_COOKIE['uid'];
$key = $_COOKIE['key'];

//验证用户认证信息
if(!check_userkey($uid , $key)){
	print_json(array('error'=>1 , 'msg'=>'Unauthorized'));
	die();
}

//创建该用户的上传目录
$user_dir = user_mkdir($uid);
if(strlen($user_dir) <= 0){
	print_json(array('error'=>1 , 'msg'=>'Make user_dir error'));
	die();
}

//上传文件
$file = $_FILES['file'];
$filename = md5_file($_FILES['file']['tmp_name']).'.'.pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);

if (!move_uploaded_file($_FILES['file']['tmp_name'], $user_dir.$filename)) {
	print_json(array('error'=>1 , 'msg'=>'Upload file error'));
	die();
} 

//返回结果
print_json(
	array(
		'error'=>0 , 
		'msg'=>'' , 
		'data'=>array(
			'name'=>$filename ,  
			'type'=>$_FILES['file']['type'] , 
			'size'=>$_FILES['file']['size'])));

?>